package com.nq.activaity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nq.activaity.entity.StLotterySettings;
import com.nq.activaity.service.IStLotterySettingsService;
import com.nq.activaity.vo.StLotterySettingsReq;
import com.nq.common.Result;
import com.nq.utils.DateTimeUtil;
import com.nq.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 抽奖设置表
 * @Author: jeecg-boot
 * @Date:   2024-11-05
 * @Version: V1.0
 */
@RestController
@RequestMapping("/st/stLotterySettings")
@Slf4j
public class StLotterySettingsController {
	@Autowired
	private IStLotterySettingsService stLotterySettingsService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stLotterySettings
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(StLotterySettings stLotterySettings,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StLotterySettings> queryWrapper = new QueryWrapper<>();
		Page<StLotterySettings> page = new Page<StLotterySettings>(pageNo, pageSize);
		IPage<StLotterySettings> pageList = stLotterySettingsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param req
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody StLotterySettingsReq req) throws ParseException {

		StLotterySettings settings = new StLotterySettings();

		settings.setCount(req.getCount());
		settings.setType(req.getType());
		settings.setStatus(req.getStatus());
		settings.setIntegral(req.getIntegral());
		settings.setLotteryRules(req.getLotteryRules());

		String beginTime = req.getBeginTime();
		String endTime = req.getEndTime();

		settings.setBeginTime(DateTimeUtil.strToDate(beginTime));
		settings.setEndTime(DateTimeUtil.strToDate(endTime));

		settings.setCreateTime(new Date());
		stLotterySettingsService.save(settings);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param req
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody StLotterySettingsReq req) throws ParseException {

		StLotterySettings settings = new StLotterySettings();
		settings.setId(req.getId());
		settings.setCount(req.getCount());
		settings.setType(req.getType());
		settings.setStatus(req.getStatus());
		settings.setIntegral(req.getIntegral());
		settings.setLotteryRules(req.getLotteryRules());

		String beginTime = req.getBeginTime();
		String endTime = req.getEndTime();

		settings.setBeginTime(DateTimeUtil.strToDate(beginTime));
		settings.setEndTime(DateTimeUtil.strToDate(endTime));
		stLotterySettingsService.updateById(settings);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		stLotterySettingsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stLotterySettingsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		StLotterySettings stLotterySettings = stLotterySettingsService.getById(id);
		if(stLotterySettings==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stLotterySettings);
	}
//
//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param stLotterySettings
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, StLotterySettings stLotterySettings) {
//        return super.exportXls(request, stLotterySettings, StLotterySettings.class, "抽奖设置表");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, StLotterySettings.class);
//    }

}
